/*
 * Copyright (c) 2009-2013 EvTech Project
 * All rights reserved.
 * 
 * This file is part of Game packet.
 *
 * Game packet is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version.
 *
 * Game packet is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Game packet.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package fi.honkalampisaatio.game.ui;

import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.JComponent;

public class ScalableCheckBox extends JComponent implements MouseListener {
	private static final long serialVersionUID = 1409800443859554344L;
	protected BufferedImage defaultYesImage;
	protected boolean isSelected = false, mousePressed = false;
	protected String text = "";
	protected final String yesImagePath = "./settings/images/Kyllä painike.png";

	public ScalableCheckBox() {
		init();
	}

	public ScalableCheckBox(String text) {
		init();
		this.text = text;
	}
	
	protected void init()
	{
		try {
			defaultYesImage = ImageIO.read(new File(yesImagePath));
		} catch (Exception e) {
	        System.err.println("Couldn't create image: " + yesImagePath);
		}
		this.addMouseListener(this);
	}
	
	public void setText(String text)
	{
		this.text = text;
	}
	
	public String getText()
	{
		return this.text;
	}

	@Override
	public void paint(Graphics g) {
		super.paint(g);
		int w,h;
		if(defaultYesImage!=null)
		{
			h = this.getHeight();
			w = defaultYesImage.getWidth()*h/defaultYesImage.getHeight();
			if(w>this.getWidth())
			{
				w = this.getWidth();
				h = defaultYesImage.getHeight()*w/defaultYesImage.getWidth();
			}
			g.draw3DRect(0, 0,w-2, h-2, true);
			if(isSelected)
				g.drawImage(defaultYesImage, 2, 2, w-5, h-5, null);
		}
		else
		{
			h = Math.min(this.getHeight(), this.getWidth());
			w = h;
			g.draw3DRect(0, 0,w-2, h-2, true);
			if(isSelected)
			{
				g.drawLine(w/5, h-5, w-5, 2);
				g.drawLine(w/5, h-5, 2, h/3);
			}
		}
		if(text!=null)
		{
			g.drawString(text, w + 2, g.getFontMetrics().getDescent() + this.getHeight()/2);
		}
	}

	public boolean isSelected()
	{
		return isSelected;
	}

	public void setSelected(boolean selected)
	{
		isSelected = selected;
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		mousePressed = false;
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		mousePressed = true;
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		if(mousePressed)
		{
			isSelected = !isSelected;
			mousePressed = false;
			repaint();
		}
	}

}
